<?php
class XA_Data_MySQL extends XA_Data {



	public function __construct($result_resource) {
		$this->result_resource = $result_resource;
	}



	public function getRow() {
		return $this->row();
	}



	public function row() {
		return $this->result_resource->fetch_assoc();
	}



	public function getRows($index = null) {
		return $this->all($index);
	}



	public function all($index = null) {
		$return = array();
		if ($this->result_resource->num_rows) {
			$this->reset();

			if ($index) {

				while ($row = $this->result_resource->fetch_assoc()) {
					$return[$row[$index]] = $row;
				}
			} else {
				if (method_exists('mysqli_result', 'fetch_all')) {
					return $this->result_resource->fetch_all(MYSQLI_ASSOC);
				} else {
					while ($row = $this->result_resource->fetch_assoc()) {
						$return[] = $row;
					}
				}
			}
		}

		return $return;
	}



	public function reset() {
		$this->result_resource->data_seek(0);
	}



	public function getRowCount() {
		return $this->result_resource->num_rows;
	}



	public function count() {
		return $this->result_resource->num_rows;
	}



	public function getValue($key = null) {
		return $this->val($key);
	}



	public function val($key = null) {
		if (!($row = $this->result_resource->fetch_assoc())) {
			return null;
		}

		return $key ? $row[$key] : current($row);
	}



	public function getValues($key = null, $index = null) {
		return $this->col($key, $index);
	}



	public function col($key = null, $index = null) {
		$this->reset();
		$return = array();

		if ($index) {
			while ($row = $this->result_resource->fetch_assoc()) {
				if (!$key) {
					$key = key($row);
				}

				$return[$row[$index]] = $row[$key];
			}
		} else {
			while ($row = $this->result_resource->fetch_assoc()) {
				if (!$key) {
					$key = key($row);
				}

				$return[] = $row[$key];
			}
		}

		return $return;
	}
}
